#####
## Replication File:
##
## Are Republicans and Conservatives More Likely
## to Believe Conspiracy Theories?
##
## Adam M. Enders, Christina Farhart, Joanne Miller,
## Joseph E. Uscinski, Kyle Saunders, & Hugo Drochon
#####

################################################################################

####
## Load necessary packages
## 
## Analyses conducted in RStudio 1.0.153,
## using R 3.6 (Mac OS)
####

library(stats)
library(car)
library(lattice)
library(latticeExtra)
library(foreign)
library(psych)

# Set working directory
setwd("~/Dropbox/CT, PID, ID Symmetry/Data and Code/Replication")

################################################################################

#####
## Figure 1
#####

# Ideology
ideocorr <- read.csv("Specific Beliefs, Ideology v2.csv", header = TRUE)

ideocorr$var <- reorder(as.factor(ideocorr$conspiracy), ideocorr$order)

plot1 <- dotplot(var ~ estimate,
        data = ideocorr,
        aspect = 1.5,
        xlab = "Correlation Coefficient",
        xlim = c(-0.71, 0.71),
        par.settings = list(clip = list(panel = FALSE)), 
        key = list(columns=3, points=list(pch=c(1,1,16,1), 
                                          col=c("black", "white", "black", "white")), 
                   text=list(text=c("Symbolic Ideology", "", "Partisanship", ""), 
                             col=c("black", "white", "black", "white"))),
        panel = function(x, y, subscripts, ...){
          panel.abline(v = 0, col = "dark grey")
          panel.dotplot(x, y, col="black", lty = 3, pch = 1)
          panel.segments(ideocorr$lower[subscripts], ideocorr$var,
                         ideocorr$upper[subscripts], ideocorr$var, col = "black")
          panel.arrows(-0.02, 54, -0.7, 54, col = "blue3", lwd=1.5)
          panel.arrows(0.02, 54, 0.7, 54, col = "red3", lwd=1.5)
          panel.text(-0.3, 55, label = "Liberal/Dem", col = "blue3")
          panel.text(0.3, 55, label = "Conservative/Rep", col = "red3")
        }
)

# Partisanship
pidcorr <- read.csv("Specific Beliefs, Partisanship v2.csv", header = TRUE)

pidcorr$var <- reorder(as.factor(pidcorr$conspiracy), pidcorr$estimate)

plot2 <- dotplot(var ~ estimate,
        data = pidcorr,
        aspect = 1.5,
        xlab = "Correlation Coefficient",
        xlim = c(-0.71, 0.71),
        par.settings = list(clip = list(panel = FALSE)), 
        panel = function(x, y, subscripts, ...){
          #panel.abline(v = 0, col = "dark grey")
          panel.dotplot(x, y, col="black", levels.fos=NA, pch = 16)
          panel.segments(pidcorr$lower[subscripts], pidcorr$var,
                         pidcorr$upper[subscripts], pidcorr$var, col = "black")
          #panel.arrows(-0.02, 36.5, -0.68, 36.5, col = "blue3")
          #panel.arrows(0.02, 36.5, 0.68, 36.5, col = "red3")
          #panel.text(-0.3, 37, label = "Democrat", col = "blue3")
          #panel.text(0.3, 37, label = "Republican", col = "red3")
        }
)

tiff("figure1.tiff", width = 7.5, height = 8, units = "in", res=300, compression = 'lzw')
plot1 + plot2
dev.off()

################################################################################

#####
## Figure 2
#####

cross <- read.csv("Cross-national Correlations.csv", header = TRUE)

tiff("figure2.tiff", width = 13, height = 9, units = "in", res=300, compression = 'lzw')
dotplot(as.factor(conspiracy) ~ corr | as.factor(country),
        data = cross,
        aspect = 1.5,
        xlab = "Correlation with Left-Right Ideology",
        xlim = c(-0.5, 0.5),
        panel = function(x, y, subscripts, ...){
                panel.abline(v = 0, col = "dark grey")
                panel.dotplot(x, y, col = "black", lty = 3)
                panel.segments(cross$lower[subscripts], cross$conspiracy[subscripts],
                               cross$upper[subscripts], cross$conspiracy[subscripts], 
                               col = "black")
        }
)
dev.off()

################################################################################

#####
## Figure 3
#####

content <- read.csv("Content-Controlled Correlations.csv", header = TRUE)

content$var <- reorder(as.factor(content$label), -1*content$order)

tiff("figure3.tiff", width = 7, height = 5.5, units = "in", res=300, compression = 'lzw')
dotplot(var ~ abs(estimated) | correlate,
        data = content,
        aspect = 1.5,
        xlab = "abs(Correlation with Partisanship and Ideology)",
        xlim = c(0.12, 0.72),
        key=list(columns=1, points=list(pch=16, col=c("red3", "blue3")), 
                 text=list(c("Democratic conspirator, Republicans likely to believe", 
                             "Republican conspirator, Democrats likely to believe"))),
        panel = function(x, y, subscripts, ...) {
                panel.dotplot(x, y, col=c("red3"), lty = 3)
                panel.segments(content$lowerd[subscripts], content$var[subscripts],
                               content$upperd[subscripts], content$var[subscripts], 
                               col = c("red3"))
                panel.dotplot(abs(content$estimater[subscripts]), content$var[subscripts], 
                              col="blue3", levels.fos=NA)
                panel.segments(abs(content$lowerr[subscripts]), content$var[subscripts],
                               abs(content$upperr[subscripts]), content$var[subscripts], 
                               col = c("blue3"))
        }
)
dev.off()

################################################################################

#####
## Figure 4
#####

conthink <- read.csv("Correlation with Conspiracy Thinking v2.csv", header = TRUE)

conthink$var <- reorder(as.factor(conthink$conspiracy), conthink$order)

tiff("figure4.tiff", width = 7, height = 7, units = "in", res=300, compression = 'lzw')
dotplot(var ~ estimate,
        data = conthink,
        aspect = 1.5,
        xlab = "Correlation w/ Conspiracy Thinking",
        xlim = c(-0.1, 0.71),
        panel = function(x, y, subscripts, ...){
                panel.dotplot(x, y, col="black", pch = 16, lty=3)
                panel.abline(v=0, col="dark grey")
                panel.segments(conthink$lower[subscripts], conthink$var,
                               conthink$upper[subscripts], conthink$var, 
                               col = "black")
                 }
)
dev.off()

################################################################################

#####
## Figure 5
#####

corrs <- read.csv("Correlations Over Time, v2.csv", header = TRUE)

corrs$var <- reorder(as.factor(corrs$label), -1*corrs$order)

text <- c(expression(bar(r)=="0.04"), expression(bar(r)=="0.03"), 
          expression(bar(r)=="0.09"))

tiff("figure5.tiff", width = 10, height = 6, units = "in", res=300, compression = 'lzw')
dotplot(var ~ corr | item,
        data = corrs,
        aspect = 1.5,
        xlab = "Correlation w/ Conspiracy Thinking",
        xlim = c(-0.25, 0.35),
        index.cond = list(c(3, 2, 1)),
        par.settings = list(clip = list(panel = FALSE)), 
        panel = function(x, y, subscripts, ...){
                panel.abline(v = 0, col = "dark grey")
                panel.dotplot(x, y, col="black", lty = 3)
                panel.segments(corrs$lower[subscripts], corrs$var,
                               corrs$upper[subscripts], corrs$var, 
                               col = "black")
                panel.text(-0.15, 1.5, label = text[panel.number()],
                           col = "black")
        }
)
dev.off()

################################################################################

#####
## Figure A1
#####

levels <- read.csv("Mean Levels, Partisanship v2.csv", header = TRUE)

levels$var <- reorder(as.factor(levels$conspiracy), levels$order)

tiff("figureA1.tiff", width = 7, height = 8, units = "in", res=300, compression = 'lzw')
dotplot(var ~ dem,
        data = levels,
        aspect = 1.5,
        groups = who,
        xlab = "Proportion Believe",
        xlim = c(-0.02, 0.97),
        #col = c("red3", "blue3", "chartreuse4"),
        #par.settings = list(clip = list(panel = FALSE)),         
        key = list(columns=3, 
                 text=list(text=c("Democrat", "Independent", "Republican"), 
                           col=c("blue3", "chartreuse4", "red3"))),
        panel = function(x, y, subscripts, ...){
                panel.dotplot(x, y, col = "white", lty = 3)
                panel.xyplot(levels$dem, levels$var, pch = 16, col = "blue3")
                panel.segments(levels$lowerd, levels$var,
                               levels$upperd, levels$var, col = "blue3")
                panel.xyplot(levels$ind, levels$var, pch = 16, col = "chartreuse4")
                panel.segments(levels$loweri, levels$var,
                               levels$upperi, levels$var, col = "chartreuse4")
                panel.xyplot(levels$rep, levels$var, pch = 16, col = "red3")
                panel.segments(levels$lowerr, levels$var,
                               levels$upperr, levels$var, col = "red3")
                 }
)
dev.off()

################################################################################

#####
## Figure A2
#####

levels <- read.csv("Mean Levels, Partisanship v2 alt.csv", header = TRUE)

levels$var <- reorder(as.factor(levels$conspiracy), levels$order)

tiff("figureA2.tiff", width = 8, height = 9, units = "in", res=300, compression = 'lzw')
barchart(var ~ estimate,
        data = levels,
        aspect = 1.5,
        groups = group,
        xlab = "Proportion Believe",
        xlim = c(-0.02, 0.87),
        col = c("blue3", "red3"),
        key = list(columns=2, 
                   text=list(text=c("Democrat", "Republican"), 
                             col=c("blue3", "red3"))),
)
dev.off()

################################################################################

#####
## Figure A3
#####

alpha <- read.csv("Alpha and Variance Explained, v2.csv", header = TRUE)

alpha$var <- reorder(as.factor(alpha$label), -1*alpha$order)

tiff("figureA3.tiff", width = 7, height = 5, units = "in", res=300, compression = 'lzw')
dotplot(var ~ estimate | quant,
        data = alpha,
        aspect = 1.5,
        xlab = " ",
        xlim = c(0.68, 0.99),
        panel = function(x, y, subscripts, ...){
                panel.abline(v = 0, col = "dark grey")
                panel.dotplot(x, y, col="black", lty = 3, pch = 16)
        }
)
dev.off()

################################################################################

#####
## Figure A4
#####

october2020 <- read.dta("Qualtrics Oct 2020.dta")

may2021 <- read.dta("Qualtrics 2021.dta")

# May 2021
m.may2021.1 <- lm(conthink ~ pid + ideo, data = may2021)
m.may2021.2 <- lm(conthink ~ pid + I(pid^2) + ideo + I(ideo^2), data = may2021)
summary(m.may2021.2)
anova(m.may2021.1, m.may2021.2)

tiff("figureA4-1.tiff", width=5, height=5, units = "in", res=300, compression = 'lzw')
set.seed(1234)
xyplot(jitter(conthink) ~ jitter(pid),
       data = may2021,
       aspect = 1,
       col = "grey",
       ylab = "Conspiracy Thinking",
       xlab = "Partisanship",
       main = "Qualtrics 2021 (May)",
       scales = list(x = list(at=1:7)),
       panel = function(x, y, ...){
               panel.xyplot(x, y, col=...)
               panel.lmline(x, y, col = "red")
               panel.smoother(x, y, method = "loess")
       }
)
dev.off()

tiff("figureA4-2.tiff", width=5, height=5, units = "in", res=300, compression = 'lzw')
set.seed(1234)
xyplot(jitter(conthink) ~ jitter(ideo),
       data = may2021,
       aspect = 1,
       col = "grey",
       ylab = "Conspiracy Thinking",
       xlab = "Ideological Self-identification",
       main = "Qualtrics 2021 (May)",
       scales = list(x = list(at=1:7)),
       panel = function(x, y, ...){
               panel.xyplot(x, y, col=...)
               panel.lmline(x, y, col = "red")
               panel.smoother(x, y, method = "loess")
       }
)
dev.off()

# October 2020
m.october2020.1 <- lm(conthink ~ pid + ideo, data = october2020)
m.october2020.2 <- lm(conthink ~ pid + I(pid^2) + ideo + I(ideo^2), data = october2020)
summary(m.october2020.2)
anova(m.october2020.1, m.october2020.2)

tiff("figureA4-3.tiff", width=5, height=5, units = "in", res=300, compression = 'lzw')
set.seed(1234)
xyplot(jitter(conthink) ~ jitter(pid),
       data = october2020,
       aspect = 1,
       col = "grey",
       ylab = "Conspiracy Thinking",
       xlab = "Partisanship",
       main = "Qualtrics 2020 (October)",
       scales = list(x = list(at=1:7)),
       panel = function(x, y, ...){
               panel.xyplot(x, y, col=...)
               panel.lmline(x, y, col = "red")
               panel.smoother(x, y, method = "loess")
       }
       )
dev.off()

tiff("figureA4-4.tiff", width=5, height=5, units = "in", res=300, compression = 'lzw')
set.seed(1234)
xyplot(jitter(conthink) ~ jitter(ideo),
       data = october2020,
       aspect = 1,
       col = "grey",
       ylab = "Conspiracy Thinking",
       xlab = "Ideological Self-identification",
       main = "Qualtrics 2020 (October)",
       scales = list(x = list(at=1:7)),
       panel = function(x, y, ...){
               panel.xyplot(x, y, col=...)
               panel.lmline(x, y, col = "red")
               panel.smoother(x, y, method = "loess")
       }
)
dev.off()
